/*
 * Home.java
 *
 * Created on January 29, 2007, 11:11 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package ge.info.pages;

import ge.info.domain.Country;
import ge.ktapestry.pages.BasePageExt;
import ge.info.domain.currency.Currency;
import ge.ktapestry.propertyselections.ComboList;
import java.util.Date;
import java.util.List;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;

public abstract class CurrencyPage extends BasePageExt implements PageBeginRenderListener
{

    @Persist("client")
    public abstract Country getCountry();

    public abstract void setCountry(Country country);

    @Persist("client")
    public abstract Currency getFromCurrency();

    public abstract void setFromCurrency(Currency currency);

    @Persist("client")
    public abstract Currency getToCurrency();

    public abstract void setToCurrency(Currency currency);

//    @Persist("client")
    public abstract String getCountryCurrencyName();

    public abstract void setCountryCurrencyName(String s);

    public static int SERVICE_ID = 11;

    public abstract Integer getAmount();

    public abstract void setAmount(Integer amount);

    @Persist("client")
     public abstract Date getScDate();

    public abstract void setScDate(Date date);

    public abstract Double getDRate();

    public abstract void setDrate(Double rate);

    public abstract Double getConvertedAmount();

    public abstract void setConvertedAmount(Double amount);

    public abstract List<String> getCurrencyNames();

    public abstract void setCurrencyNames(List<String> list);

    public abstract String getCurrencyName();

    public abstract void setCurrencyName(String s);

    public void pageBeginRender(PageEvent event)
    {
        if (!event.getRequestCycle().isRewinding())
        {
            if (getFromCurrency() == null)
            {
                Currency c = new Currency();
                c.setCurrencyID(1);
                setFromCurrency(c);
                c = new Currency();
                c.setCurrencyID(1);
                setToCurrency(c);
                 if (getScDate() != null)
        {
            c.setCdate(getScDate());
        }
            }
            if (getCountry() == null)
            {
                Country c = new Country();
                c.setCountryID(194);
                setCountry(c);
                setCountryCurrencyName("GEL");
            }
        }

    }

    private Currency getCurrencyFromID(int id)
    {
        List list = ((ComboList) getComboes().getCombo("Currencies")).getOptions();
        Currency currency = new Currency();
        for (Object o : list)
        {
            if (((Currency) o).getCurrencyID() == id)
            {
                Currency cc = (Currency) o;
                currency.setCurrencyID(cc.getCurrencyID());
                currency.setCurName(cc.getCurName());
                currency.setCountryID(cc.getCountryID());
                 if (getScDate() != null)
        {
            currency.setCdate(getScDate());
        }
                break;
            }
        }
        setAmount(null);
        return currency;
    }
    public void changeFromCurrency(IRequestCycle cycle, String id)
    {
        setFromCurrency(getCurrencyFromID(Integer.parseInt(id)));



    }
    public void changeToCurrency(IRequestCycle cycle, String id)
    {
        setToCurrency(getCurrencyFromID(Integer.parseInt(id)));
    }

    public void convert(IRequestCycle cycle)
    {
        if (!getFromCurrency().equals(getToCurrency()))
        {

            /*
             if (getScDate() != null)
        {

            this.getDbExecutor().setDRates(getFromCurrency(), getToCurrency(), getSession());

            this.getDbExecutor().setDMrates(getFromCurrency(), getToCurrency(), getSession());
            this.getDbExecutor().setDSmrates(getFromCurrency(), getToCurrency(), getSession());
 *
 *  
             }
             else
             {
           */
            this.getDbExecutor().setRates(getFromCurrency(), getToCurrency(), getSession());
            
            this.getDbExecutor().setMrates(getFromCurrency(), getToCurrency(), getSession());
            this.getDbExecutor().setSmrates(getFromCurrency(), getToCurrency(), getSession());
            }

        //}
    }

    public void getCountryCurrency(IRequestCycle cycle)
    {
        List<Currency> list = (List<Currency>) ((ComboList) getComboes().getCombo("Currencies")).getOptions();
        String s = "-";
        for (Currency cur : list)
        {
            if (cur.getCountryID() == getCountry().getCountryID())
            {
                s = cur.getCurName();
            }
        }
        setCountryCurrencyName(s);
    }

    public void findCountry(IRequestCycle cycle)
    {
        if (getCurrencyName() != null)
        {
            setCurrencyNames(getDbExecutor().findCountryByCurrency(getCurrencyName()));
        }
    }
         public void sendSMS(IRequestCycle cycle, Currency currency)
    {


       StringBuilder sb = new StringBuilder();
        if (currency!=null)
        {

        sb.append(getToCurrency().getUnit()).append(" ");
        sb.append(getToCurrency().getCurName()).append(" = ");
        sb.append(getFromCurrency().convertTo(getToCurrency())).append(" ");
        sb.append(getFromCurrency().getCurName()).append(" ");

        }



        getDbExecutor().insertSms(getSession(), sb.toString(),SERVICE_ID);
    }
         public void sendSMSamount(IRequestCycle cycle, Currency currency)
    {


       StringBuilder sb = new StringBuilder();
        if (currency!=null)
        {

        sb.append(getAmount()).append(" ");
        sb.append(getToCurrency().getCurName()).append(" = ");
        sb.append(getFromCurrency().convertAmountTo(getToCurrency(),getAmount())).append(" ");
        sb.append(getFromCurrency().getCurName()).append(" ");

        }



        getDbExecutor().insertSms(getSession(), sb.toString(),SERVICE_ID);
    }
}

